<?php
App::import('Component', 'Email');

/**
 * New email component which reads email templates from the model/db rather than files
 * @author Abhimanyu
 *
 */
class GigaMailComponent extends EmailComponent
{
	
	function send($content = null, $template = null, $layout = null)
	{
		if(!is_null($template))
		{
			$this->template = $template;
		}
		$this->__createHeader();
		
		// fetch this template from db, store it in $content
		$message = $this->_fetchContent();
							
		$this->template = null;
		
		$message[] = '';
		$this->__message = $message;

		if (!empty($this->attachments)) {
			$this->__attachFiles();
		}

		if (!is_null($this->__boundary)) {
			$this->__message[] = '';
			$this->__message[] = '--' . $this->__boundary . '--';
			$this->__message[] = '';
		}

		if ($this->_debug) {
			return $this->__debug();
		}
		
		// check if this is a test session
		if($this->Session->check('tester_id'))
		{
			$this->delivery = 'db';
		}
				
		$__method = '__' . $this->delivery;
		$sent = $this->$__method();
		$this->__header = array();
		$this->__message = array();

		return $sent;
	}

	// Important: we dont use $content in EmailComponent::send() so this functionality is not added
	function _fetchContent() 
	{
		$content = null; 
		$viewClass = $this->Controller->view;

		if ($viewClass != 'View') {
			if (strpos($viewClass, '.') !== false) {
				list($plugin, $viewClass) = explode('.', $viewClass);
			}
			$viewClass = $viewClass . 'View';
			App::import('View', $this->Controller->view);
		}
		$View = new $viewClass($this->Controller, false);
		$View->layout = $this->layout;
		$msg = array();

		//$content = implode("\n", $content);

		if ($this->sendAs === 'both') {
			$htmlContent = $content;
			if (!empty($this->attachments)) {
				$msg[] = '--' . $this->__boundary;
				$msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $this->__boundary . '"';
				$msg[] = '';
			}
			$msg[] = '--alt-' . $this->__boundary;
			$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
			$msg[] = 'Content-Transfer-Encoding: 7bit';
			$msg[] = '';

			$content = $this->_getEmailTemplate($this->template, 'html');
		
			$View->layoutPath = 'email' . DS . 'html';
			$content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
		
			$msg = array_merge($msg, $content);

			$msg[] = '';
			$msg[] = '--alt-' . $this->__boundary;
			$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
			$msg[] = 'Content-Transfer-Encoding: 7bit';
			$msg[] = '';

			$htmlContent = $this->_getEmailTemplate($this->template, 'html');
			$View->layoutPath = 'email' . DS . 'html';
			$htmlContent = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($htmlContent)));
			$msg = array_merge($msg, $htmlContent);
			$msg[] = '';
			$msg[] = '--alt-' . $this->__boundary . '--';
			$msg[] = '';
			
			return $msg;
		}

		if (!empty($this->attachments)) {
			if ($this->sendAs === 'html') {
				$msg[] = '';
				$msg[] = '--' . $this->__boundary;
				$msg[] = 'Content-Type: text/html; charset=' . $this->charset;
				$msg[] = 'Content-Transfer-Encoding: 7bit';
				$msg[] = '';
			} else {
				$msg[] = '--' . $this->__boundary;
				$msg[] = 'Content-Type: text/plain; charset=' . $this->charset;
				$msg[] = 'Content-Transfer-Encoding: 7bit';
				$msg[] = '';
			}
		}

		$content = $this->_getEmailTemplate($this->template, 'html');
		$View->layoutPath = 'email' . DS . $this->sendAs;
		$content = explode("\n", str_replace(array("\r\n", "\r"), "\n", $View->renderLayout($content)));
		$msg = array_merge($msg, $content);

		return $msg;
	}
	
	function _getEmailTemplate($template, $type = 'html')
	{
	
		$conditions = array(
			'EmailTemplate.name' => $template
		);
		$template = ClassRegistry::init('Cms.EmailTemplate')->find('first', compact('conditions'));
		$msgId = 'email_body_'.$template['EmailTemplate']['name'].'_'.$type;
		$msgStr = __($msgId,true);
			
		if($msgId!=$msgStr)
		{		
			return $msgStr;
		}	
		return $template['EmailTemplate']['body_'.$type];
	}

	function __createHeader()
	{
		if($this->template &&(!$this->subject || $this->subject==''))
		{
			$this->subject = ClassRegistry::init('Cms.EmailTemplate')->field('subject',array('name'=>$this->template));
		}
		$msgId = 'email_subject_'.$this->template;
		$msgStr = __($msgId,true);
		if($msgId!=$msgStr)
		{
			$this->subject = $msgStr;
		}	
			
	parent::__createHeader();
	}
}
?>